<template>
  <div>
    <van-nav-bar title="账号登录" left-arrow />

    <van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="username"

    placeholder="请输入账号"
    :rules="[{ required: true, message: '请输入账号' },
        { required: true, message: '账号包含5-8个字符', pattern:  /^\w{5,8}$/ }]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"

    placeholder="请输入密码"
    :rules="[{ required: true, message: '请输入密码' },
        { required: true, message: '账号包含5-12个字符', pattern:  /^\w{5,12}$/ }]"
  />
  <div style="margin: 16px;">
    <van-button  block type="info" native-type="submit">登录</van-button>
  </div>
</van-form>

<p class="register" @click="fn">还没有账号，去注册~</p>
  </div>
</template>

<script>
import {login} from "@/api/user"
import {setToken} from "@/utils/storage"
export default {
  name: 'login',
   data() {
    return {
      username: '',
      password: '',
    };
  },
  methods: {
     async onSubmit (values) {
      // console.log('submit', values)
      const data= await login(values)
      console.log(data);
      setToken(data.body.token)
      this.$toast('登录成功')
      this.$router.push('/home')
    },
    fn(){
      this.$router.push('/register')
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar .van-icon {
  color: #fff;
}
.van-nav-bar__title {
    color: #fff;
}
.register {
    text-align: center;
    margin-top: 20px;
    color: 	#696969;
    font-size: 14px;
}
</style>
